package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Menu;
import cn.itcast.pojo.Permission;
import cn.itcast.pojo.UserInfo;
import cn.itcast.service.MenuService;
import cn.itcast.vo.MenuListVo;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("menu")
@Slf4j
public class MenuController {

    @Reference
    private MenuService menuService;

    /**
     * 分页查询菜单列表
     * @param queryPageBean 查询条件
     * @return
     */
    @GetMapping("page")
    public Result findPage(QueryPageBean queryPageBean){
        System.out.println("menuService = " + menuService);
        try {
            PageResult pageResult = menuService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, pageResult);
        } catch (Exception e) {
            log.error("查询菜单列表失败", e);
        }
        return new Result(false, MessageConstant.QUERY_MENU_FAIL);
    }



    /**
     * 新增菜单/新增子菜单
     * @param permissionIds 权限id数组
     * @param menu  菜单的基本数据
     * @return
     */
    @PreAuthorize("hasAuthority('MENU_ADD')")
    @PostMapping()
    public Result handleAdd(int[] permissionIds, @RequestBody Menu menu){
        try {
            Boolean flag = menuService.handleAdd(permissionIds, menu);
            if (flag){
                return new Result(true, MessageConstant.ADD_MENU_SUCCESS, flag);
            }
        } catch (Exception e) {
            log.error("新增菜单失败", e);
        }
        return new Result(false, MessageConstant.ADD_MENU_FAIL);
    }


    /**
     * 根据id查询单条菜单信息
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @GetMapping("{id}")
    public Result handleUpdate(@PathVariable("id") Integer id){
        try {
            Menu menu = menuService.handleUpdate(id);
            if (menu != null){
                return new Result(true, MessageConstant.EDIT_MENU_SUCCESS, menu);
            }
        } catch (Exception e) {
            log.error("根据id查询单条菜单信息失败", e);
        }
        return new Result(false, MessageConstant.EDIT_MENU_FAIL);
    }


    /**
     * 根据id查询单条菜单的权限信息
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @GetMapping("{id}/permissions")
    public Result handleUpdatePermissions(@PathVariable("id") Integer id){
        try {
            List<Integer> list = menuService.handleUpdatePermissions(id);
            if (list != null && list.size() > 0){
                return new Result(true, MessageConstant.EDIT_MENU_SUCCESS, list);
            }
        } catch (Exception e) {
            log.error("根据id查询单条菜单的权限信息失败", e);
        }
        return new Result(false, MessageConstant.EDIT_MENU_FAIL);
    }





    /**
     * 编辑一级菜单/编辑二级菜单
     * @param permissionIds 权限id数组
     * @param menu  菜单的基本数据
     * @return
     */
    @PreAuthorize("hasAuthority('MENU_EDIT')")
    @PutMapping()
    public Result handleEdit(int[] permissionIds, @RequestBody Menu menu){
        try {
            Boolean flag = menuService.handleEdit(permissionIds, menu);
            if (flag){
                return new Result(true, MessageConstant.EDIT_MENU_SUCCESS, flag);
            }
        } catch (Exception e) {
            log.error("修改菜单失败", e);
        }
        return new Result(false, MessageConstant.EDIT_MENU_FAIL);
    }




    /**
     * 根据id删除菜单
     * @param id  菜单的基本数据
     * @return
     */
    @PreAuthorize("hasAuthority('MENU_DELETE')")
    @DeleteMapping("{id}")
    public Result handleDelete(@PathVariable("id") Integer id){
        try {
            Boolean flag = menuService.handleDelete(id);
            if (flag){
                return new Result(true, MessageConstant.DELETE_MENU_SUCCESS, flag);
            }
        } catch (Exception e) {
            log.error("根据id删除菜单失败", e);
        }
        return new Result(false, MessageConstant.DELETE_MENU_FAIL);
    }


    /**
     * 动态展示菜单
     * @return
     */
    @GetMapping("/findMenuByUserId")
    public Result findMenuByUserId(){
        try {
            UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            List<MenuListVo> menuListVo = menuService.findMenuByUserId(userInfo.getId());
            if (menuListVo != null && menuListVo.size() > 0 ) {
                return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menuListVo);
            }

        } catch (Exception e) {
            log.error(MessageConstant.QUERY_MENU_FAIL, e);
        }
        return new Result(false, MessageConstant.QUERY_MENU_FAIL);
    }

}
